package business.inhospitalqc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;

import static common.Login.login;
import static common.TestStep.requestAndRespondBody;

public class CreateTemplate {
    Logger log = Logger.getLogger(CreateTemplate.class);
    public static final String timestamp = String.valueOf(System.currentTimeMillis());
    public static final String system = "env.properties";
    Data data = login();

    /*
    *  api-sourcedata/dmp/template/create的datas
    * @Description: 住院质控-发起质控评价-指标模板新增
     * @param i
    * @return java.lang.String
    * @author gujinle
    * @date 2021-09-07 16:25
    */
    public String CreateModel(int i) throws ConfigurationException, FileNotFoundException, YamlException {
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
/*      json.put("groupId", "100");
        json.put("timeScope", "2");    //
        json.put("status", "2");
        json.put("version", "7");    //版本：4数据目录筛选，5天塔筛选，6科研/临床/病例讨论筛选，7质控2.0  10事件筛选  11 队列筛选
        json.put("dataIds", "248,249,258,3502,724,893,2852,2909,2781,4310,1516,1519,1523,511,1560,1548,5771,5772,15140,15141,2884,2887,2898,2908,1555,3294,5638,5639,5640,5641,5642,5643,5644,5645,5646,5647,5648,5649,5650,5292,4161,4316,5774,14968,1513,909,3445,1171,3163,15070,15071,15072,15073,3530,3531,3532,3533,3534,3535,3536,15937");
        json.put("whereResult", "{\"logicSymbol\":\"and\",\"whereList\":[{\"logicSymbol\":\"or\",\"whereType\":1,\"whereList\":[{\"logicSymbol\":\"or\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"1\",\"dataId\":\"463\",\"dataRId\":\"415\",\"columnValue\":\"2018-04-01,2018-12-31\",\"columnName\":\"INDEX_TIME\",\"columnTitle\":\"住院\",\"columnType\":\"date\",\"dataName\":\"出院时间\"}],\"nextLogicSymbol\":\"and\"},{\"logicSymbol\":\"or\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"2\",\"dataId\":\"1519\",\"dataRId\":\"1433\",\"columnValue\":\"是\",\"columnName\":\"AECOPD\",\"columnTitle\":\"慢性阻塞性肺疾病急性加重期\",\"columnType\":\"string\",\"dataName\":\"是否AECOPD患者\"}]}]}]}");
        json.put("dataScope", "1");      //	 类型：0全部，1住院，2门诊
        json.put("type", "20");     //类型：0初始模板，1队列模板，10数据目录模板，20数据集
        json.put("templateName", "初始版本" + timestamp);
        json.put("indexRule", "1");     //类型： 0全部，1第一次，2最后一次
        json.put("randomNum", "10");
        json.put("businessVariables", "{\"dataType\":\"5\",\"dataId\":\"5\",\"OPTION\":true}");
        */

        json.put("groupId", yaml.getDatas().get(i).get("groupId"));
        json.put("timeScope", yaml.getDatas().get(i).get("timeScope"));    //
        json.put("status", yaml.getDatas().get(i).get("status"));
        json.put("version",yaml.getDatas().get(i).get("version"));    //版本：4数据目录筛选，5天塔筛选，6科研/临床/病例讨论筛选，7质控2.0  10事件筛选  11 队列筛选
        json.put("dataIds", yaml.getDatas().get(i).get("dataIds"));
        json.put("whereResult", yaml.getDatas().get(i).get("whereResult").toString());
        json.put("dataScope", yaml.getDatas().get(i).get("dataScope"));      //	 类型：0全部，1住院，2门诊
        json.put("type", yaml.getDatas().get(i).get("type"));     //类型：0初始模板，1队列模板，10数据目录模板，20数据集
        json.put("templateName", "初始版本" + timestamp);
        json.put("indexRule", yaml.getDatas().get(i).get("indexRule"));     //类型： 0全部，1第一次，2最后一次
        json.put("randomNum", yaml.getDatas().get(i).get("randomNum"));
        json.put("businessVariables", yaml.getDatas().get(i).get("businessVariables").toString());

        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "post", yaml.getUri());
        String jsonStr = response.asString();
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath + yaml.getUri();
//        log.info("访问链接是："+url);

        requestAndRespondBody(url,json,jsonStr);
        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String templateId = jsonObject.getJSONObject("data").getString("id");
        return templateId;   //返回templateid
    }


    public  void a() throws ConfigurationException, FileNotFoundException, YamlException {
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        String method= yaml.getMethod();
        JSONObject a=  (JSONObject)yaml.getDatas().get(0).get("whereResult");
        String e=yaml.getDatas().get(0).get("businessVariables").toString();
        String uri=yaml.getUri();
        System.out.println( e+" 信息"+a.toJSONString());
    }

    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {
        CreateTemplate t=new CreateTemplate();
        t.CreateModel(0);

    }
}
